using System;
using System.Collections;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    public class DMPropertyNameRule : IBusinessObjectRule
    {
//        private readonly DMProperty _dmProperty;
        private string propertyName;
        private string propertyClassName;
        private ArrayList reservedWords;

        public DMPropertyNameRule()
        {
//            if (dmProperty == null) throw new ArgumentNullException("dmProperty");
//            _dmProperty = dmProperty;
//            SetReservedWordsList();
        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
//            if (String.IsNullOrEmpty(_dmProperty.PropertyName)) return true;
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            SetReservedWordsList();
            DMProperty dmProperty = (DMProperty) bo;
            propertyName = dmProperty.PropertyName;
            propertyClassName = dmProperty.ClassName;
            if (String.IsNullOrEmpty(propertyName)) return true;
            return Valid();
        }

        private bool Valid()
        {
            return !reservedWords.Contains(propertyName.ToUpper());
        }

        private void SetReservedWordsList()
        {
            reservedWords = new ArrayList();
            reservedWords.Add("DIRTYXML");
            reservedWords.Add("RELATIONSHIPS");
            reservedWords.Add("PROPS");
            reservedWords.Add("CLASSDEF");
            reservedWords.Add("STATUS");
            reservedWords.Add("ID");
            reservedWords.Add("TOSTRING");
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMPropertyNameRule"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get { return "The Property Name '" + propertyName + "' on class '" + propertyClassName + "' is not valid as it is a reserved Property Name."; }
        }
        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Error; }
        }

        #endregion
    }
}